package com.yc.thread;

//线程组：批量管理线程的或线程组对象
public class Test15_threadgroup {
    public static void main(String[] args) {
        //创建任务对象
        TestThread task1= new TestThread();
        TestThread task2= new TestThread();
        ThreadGroup threadGroup = new ThreadGroup("新建线程组1");

        Thread t0 = new Thread(threadGroup, task1);
        Thread t1 =new Thread(threadGroup,task2);
        t0.start();
        t1.start();
        System.out.println("活动的线程数为："+ threadGroup.activeCount());
        System.out.println("活动的线程的名称："+ threadGroup.getName());
        //*******线程组中断，则这个组中所有的线程全部中断
        threadGroup.interrupt();
    }
}


//任务类
class TestThread implements  Runnable{
    @Override
    public void run() {
        try{
        //判断当前线程的中断信号
        while (!Thread.currentThread().isInterrupted()) {
            System.out.println( "线程名"+Thread.currentThread().getName());
                Thread.sleep(1000);
            }
        }catch (InterruptedException e) { //因为catch所以中断信号又恢复成了  false
                e.printStackTrace();
            }
        }


    }